package gcommand

import (
	"encoding/json"
	"io/ioutil"
)

var TheConfig *CommandConfig = &CommandConfig{
	"",
	&MainConfig{
		PosX: 200, PosY: 100,
		RoostHost: "127.0.0.1:9000",
	},
}

type CommandConfig struct {
	filePath string
	Config   *MainConfig
}
type MainConfig struct {
	PosX      int
	PosY      int
	RoostHost string
	Account   string
	Password  string
}

func (this *CommandConfig) Init(path string) error {
	this.filePath = path
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	return json.Unmarshal(data, this.Config)
}
func (this *CommandConfig) Save() {
	this.Config.RoostHost = TheModelLogin.RoostHost
	this.Config.Account = TheModelLogin.Account
	this.Config.Password = TheModelLogin.Password
	b, _ := json.Marshal(this.Config)
	ioutil.WriteFile(this.filePath, b, 0666)
}
func (this *CommandConfig) Valid() bool {
	return this.Config.Account != "" && this.Config.Password != ""
}
